// packagMy/pages/userFootprint/userFootprint.js
import { queryForPartnerByPage, customerOne } from '../../../config/config'
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: getApp().globalData.imgUrl,
    videoUrl: getApp().globalData.videoUrl,
    active: 0,
    topBgimg: 'https://gcenterimg.quanyou.com.cn/img_source/footprint_detail_bg.png',
    titleHeight: '',
    marginTop: '',
    typeData: [{id: 0, name: '全部'}, {id: 1, name: '商品'}, {id: 2, name: '内容'}],
    listCur: 0,
    page: 1,
    rows: 10,
    nomoreData: false,
    id: '',
    listData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.getSystemInfo({
      success(res) {
        let statusBarHeight = res.statusBarHeight
        let marginTop = res.statusBarHeight + 105
        that.setData({
          titleHeight: statusBarHeight + 'px',
          marginTop: marginTop + 'px'
        })
      }
    })
    if (options.id) {
      that.setData({
        id: options.id
      })
      //等待定位完成后再执行
      var stInFun = setInterval(() => {
        if (wx.getStorageSync('loginMsg').token && wx.getStorageSync('addrCode').provinceCode) {
          clearInterval(stInFun)
          that.getUserInfo(options.id)
          that.getList(options.id, 0)
        }
      }, 500);
    }
  },
  // 返回上一页
  goreBack() {
    wx.navigateBack({
      delta: 1,
    })
  },
  goHome() { //返回主页
    wx.reLaunch({
      url: '/pages/newIndex/newIndex'
    })
  },
  listSelect(e) {
    let listCur = e.currentTarget.dataset.id
    this.setData({
      listCur: listCur,
      page: 1,
      rows: 10,
      nomoreData: false,
      listData: []
    })
    if (listCur == 0) {
      this.getList(this.data.id, 0)
    } else if (listCur == 1) {
      this.getList(this.data.id, 1)
    } else {
      this.getList(this.data.id, 2)
    }
  },
  onChange(event) {
    wx.showToast({
      title: `切换到标签 ${event.detail.name}`,
      icon: 'none',
    });
  },
    // 获取足迹列表
    getList(id, listCur) {
      let that = this
      let formdata = {}
      formdata.page = that.data.page
      formdata.rows = that.data.rows
      if (listCur == 0) {
        formdata.filtersRaw = []
      } else if (listCur == 1) {
        formdata.filtersRaw = JSON.stringify([{"id":"type0","value":"product","property":"type","operator":"="}])
      } else {
        formdata.filtersRaw = JSON.stringify([{"id":"type0","value":"[uFound,uShow,encyclopedia]","property":"type","operator":"in"}])
      }
      app.reqFun.requestForm('POST',queryForPartnerByPage+'/'+id,formdata,(res)=>{
        if(res.data.success === 1) {
          let records = res.data.data.records
          for (var i = 0, l = records.length; i < l; i++) {
            for (var j = 0, k = records[i].appUmsMemberFootprintDataDtos.length; j < k; j++) {
              if (records[i].appUmsMemberFootprintDataDtos[j].type === 'videoInfo' && records[i].appUmsMemberFootprintDataDtos[j].img) {
                if (records[i].appUmsMemberFootprintDataDtos[j].img.split('/')[0] === 'marketing') {
                  records[i].appUmsMemberFootprintDataDtos[j].img = this.data.videoUrl + records[i].appUmsMemberFootprintDataDtos[j].img
                } else {
                  records[i].appUmsMemberFootprintDataDtos[j].img = this.data.imgUrl + records[i].appUmsMemberFootprintDataDtos[j].img
                }
              }
            }
          }
          if (res.data.data.total < that.data.rows * that.data.page) {
            that.setData({
              nomoreData: true
            })
          }
          that.setData({
            listData: that.data.listData.concat(records)
          })
          console.log('listData', this.data.listData)
        }
      })
    },
    // 获取头部用户信息
    getUserInfo(id) {
      let that = this
      app.reqFun.requestJson('GET',customerOne+'/'+id,'',(res)=>{
        if(res.data.success === 1) {
          let userInfo = res.data.data
          userInfo.bind = true
          that.setData({
            userInfo: userInfo
          })
        } else {
          let userInfo = {
            bind: false
          }
          that.setData({
            userInfo: userInfo
          })
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      })
    },
    miniJumpPage(e) {
      let miniJumpUrl = e.currentTarget.dataset.key.miniJumpUrl
      wx.navigateTo({
        url: miniJumpUrl,
      })
    },
    
  //拨打电话
  callPhone: function (e) {
    let phone = e.currentTarget.dataset.phone
    if (phone) {
      wx.makePhoneCall({
        phoneNumber: phone,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.nomoreData) {
      this.setData({
        page: this.data.page + 1
      })
      this.getList(this.data.id, this.data.listCur)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})